<?php
class admin_faqcate extends CI_Controller {
	protected $_page_name = "admin_faqcate";
	protected $_page_id = 'ul_postcontainer';
	protected $data; //data for this page.
	protected $submitdata; //data submit.
	protected $_page_title = ADMINTITLE_FAQCATE;
	protected $_base_url;
	
	function  __construct()  {
		$this->_initdata();
	}
	
	//Controller: default
	function index() {
		//1. get danh muc tin (faq_cate)
		$this->data['aFaqCate'] = $this->index_get_faq_cate_list();
		//2. reset message
		$this->data['msg'] = $_SESSION['msg'];
		$_SESSION['msg'] = "";
		//3. show to browser.
		$this->showadmin($this->data);
	}		
	
	//Controller: edit
	function edit()
	{
		if (!empty($_POST['isPost']) && ($_POST['isPost']==1)) {
			$id =  $_POST['h_id'];
			if ($id != "")
			{
				$this->submitdata['cate_name'] =  $_POST['txt_cate_name'];
				$this->submitdata['url'] =  convertToVn(trim($_POST['txt_cate_name']));
				$this->submitdata['status'] =  $_POST['cbo_Status'];									
				//update DB
				$this->Adminmodel->_update_faqcate('faq_cate', "cate_id", $_POST['h_id'], $this->submitdata);
				//write log	
				$this->Adminmodel->_write_log("Action,Step,Info", 
																			"update news,update record,newsid=".$id."|IP=".$_SERVER['REMOTE_ADDR']."|user_be=".$this->Adminmodel->_get_session(SESSION_ADMINUSERNAME));				
			}
			//thong bao & redirect page
			$_SESSION['msg'] = "Bạn đã cập nhật ".$this->_page_title." thành công.";
			$this->load->helper('url');
			echo "<script>window.location='".$this->data['base_url'].'admin_faqcate/1'."'</script>";				
		}		
		else
		{
			$this->data["page_name"]="admin_faqcate_edit";
			$this->data['faqcate_detail'] = $this->Adminmodel->_get_faqcate_detail($this->uri->segment(3));	
			$this->data['aFaqCate'] = $this->Adminmodel->_get_faq_cate_list();
			$this->data['id'] = $this->uri->segment(3);
			$this->data['previous_id'] = 1;			
			$this->data['img_url'] = 1;		
			$this->data['page_title']=$this->_page_title;			
			$this->load->view('common/common_view_admin',$this->data);					
		}
	}
	
	//Controller: add
	function add()
	{
		$this->data["page_name"]="admin_faqcate_add";
		$this->data['previous_id'] = 1;
		$this->data['aFaqCate'] = $this->Adminmodel->_get_faq_cate_list();			
		$this->load->view('common/common_view_admin',$this->data);
		if (!empty($_POST['isPost']) && ($_POST['isPost']==1)) {
			//get submit data
			$this->submitdata['cate_name'] =  $_POST['txt_cate_name'];
			$this->submitdata['url'] =  convertToVn(trim($_POST['txt_cate_name']));
			$this->submitdata['status'] =  $_POST['cbo_Status'];
			
			//insert faqcate
			$insert_id = $this->Adminmodel->_insert_faqcate('faq_cate', $this->submitdata);	

			//write log	
			$this->Adminmodel->_write_log("Action,Step,Info", 
																		"insert news,insert DB,newsid=".$insert_id."|IP=".$_SERVER['REMOTE_ADDR']."|user_be=".$this->Adminmodel->_get_session(SESSION_ADMINUSERNAME));				
						
			//thong bao & redirect page
			$_SESSION['msg'] = "Bạn đã thêm mới ".$this->_page_title." thành công.";
			$this->load->helper('url');
			echo "<script>window.location='".$this->data['base_url'].'admin_faqcate'."'</script>";				
		}
	}
	
	//Controller: delete
	function delete()
	{
		$id=($this->uri->segment(3)!="")?$this->uri->segment(3):0;
		if ($id==0)
		{
			echo "fail";
		}
		else
		{
			//get news details.
			$aNewsDetails = $this->Adminmodel->_get_news_detail($id);
			//move to deleted folder.
			$this->delete_news_image($aNewsDetails[0]->img, $aNewsDetails[0]->img_thumb);
			//write log.
			$this->Adminmodel->_write_log("Action,Step,Info", 
																		"delete news,delete record,newsid=".$id."|IP=".$_SERVER['REMOTE_ADDR']."|user_be=".$this->Adminmodel->_get_session(SESSION_ADMINUSERNAME));			
			echo $this->Adminmodel->_delete_news($id);
		}
	}		
	
	//local func:
	function delete_news_image($img, $img_thumb)
	{
		$this->invoke_delete_image($img);
		$this->invoke_delete_image($img_thumb);		
	}
	
	//local func:
	function invoke_delete_image($img)
	{
		if ($img != "")
		{
			if (file_exists("./style/".$img))
			{
				$a = explode("/", $img);
				if (count($a) > 1)
				{
					if (copy("./style/".$img, IMAGE_ROOT.IMAGE_DIR."news/deleted/".$a[count($a)-1]))
						unlink("./style/".$img);
				}
			}
		}
	}
	
	//local func:
	function index_get_news_list()
	{
		$iCurPage=1;
		$iCurPage = $this->uri->segment(2);
		$iRow_Count=0;
		@$limit=PAGESIZE;
		$start=$iCurPage*PAGESIZE-PAGESIZE;
		if(!($start > 0)) { 
		$start = 0;
		}
		$eu = ($start - 0); 
		if(!$limit > 0 ){ 
		$limit = 20; 
		}                             
		list($iRow_Count, $this->data['aNews']) = $this->Adminmodel->_get_news_list($eu, $limit);
		$this->data['paging'] = $this->paging($iCurPage,ceil($iRow_Count/PAGESIZE),$this->config->item('base_url').$this->_page_name,$iRow_Count);
		$this->data['cur_page'] = $iCurPage;
		$this->data['previous_id'] = 1;			
		$this->data['img_url'] = 1;			
	}
	
	//local func
	function index_get_faq_cate_list()
	{
		return $this->Adminmodel->_get_faq_cate_list();
	}
	
	//local func: init data
	function _initdata()
	{
		parent::__construct();
		session_start();
		$this->load->model('Adminmodel');
		$this->load->helper('url');
		$this->_check_admin_login();
		
		//init data (this page, header admin)
		$this->_base_url = $this->config->item('base_url');
		$this->data['base_url'] = $this->config->item('base_url');
		$this->data['page_title'] = $this->_page_title;
		$this->data['admintitle_news'] = ADMINTITLE_NEWS;
                $this->data['admintitle_news_auto'] = ADMINTITLE_NEWS_AUTO;
                $this->data['admintitle_video'] = ADMINTITLE_VIDEO;
                $this->data['admintitle_customs'] = ADMINTITLE_CUSTOMS;
		$this->data['admintitle_adv'] = ADMINTITLE_ADV;
		$this->data['admintitle_faq'] = ADMINTITLE_FAQ;
		$this->data['admintitle_contact'] = ADMINTITLE_CONTACT;
		$this->data['admintitle_common'] = ADMINTITLE_COMMON;
		$this->data['admintitle_stats'] = ADMINTITLE_STATS;
		$this->data['admintitle_beuser'] = ADMINTITLE_BEUSER;
		$this->data['website_name'] = WEBSITE_NAME;		
		$this->data['link_page_name'] = $this->_page_name;
		$this->data['is_addnew'] = "add"; //show link add / list
		$this->data['page_id'] = $this->_page_id;	
		$this->data['page_name'] = $this->_page_name;
	}			
	
	//local func: check admin login
	function _check_admin_login()
	{
		if (($this->Adminmodel->_check_login(SESSION_ADMINUSERNAME) == false) ||
				(time() - $this->Adminmodel->_get_session(SESSION_ADMINTIMEOUT)) > SESSION_ADMINTIMEOUTVALUE
			 )
		{
			$base_url=$this->config->item('base_url');
			redirect($base_url.'admin_login', 'location', 301);
		}		
		else
			$this->Adminmodel->_set_session(SESSION_ADMINTIMEOUT,time());
	}	
	
	//local func
	function upload_images($upload_path, $insert_id, $data_update)
	{
		//upload image1: 1 thumbed images
		list($filename, $file_type, $filename1_db) = $this->save_image($insert_id, $upload_path, "image1");
		if ($filename != "")
		{
			$filename_thumb_db = $this->Adminmodel->save_image_thumb($insert_id, IMAGE_ROOT.IMAGE_DIR.$upload_path.$filename, $filename, $file_type, $upload_path, IMAGE_X_SIZE, IMAGE_Y_SIZE, "1");
			$data_update['img']=IMAGE_DIR.$upload_path.$filename;
    	$data_update['img_thumb']=$filename_thumb_db;
		}

		return $data_update;
	}	
	
	//local func
	function save_image($insert_id, $upload_path, $upload_control_name)
	{
		return $this->Adminmodel->save_image($insert_id, $upload_path, $upload_control_name);
	}	
	
	//local func
	function paging($page, $totalpage, $pagelink, $totalrows)
	{
		$paging_str = '';
		$paging_str .= 'Trang: &nbsp;&nbsp;&nbsp;';
		if ($page > 1)
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img21_arrow_left.gif" align="absmiddle" style="cursor:pointer;" onclick="location.href=' ."'".$pagelink.'/'.($page-1)."'". '">';
		else
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img21_arrow_left.gif" align="absmiddle">';
		$paging_str .= '&nbsp;&nbsp;&nbsp;';
		
		// list 10 pagelink
		$startpage = 1;
		if ($page > 5)  $startpage = $page - 4;
		$endpage = $totalpage;
		if ($totalpage - $page > 5)  $endpage = $page + 5;
		for ($i=$startpage; $i<$page; $i++)
			$paging_str .= ' <a href="' .$pagelink.'/'.$i. '" class="link">' .$i. '</a>';
		$paging_str .= ' <a href="' .$pagelink.'/'.$i. '" class="link_selected">' .$page. '</a>';
		for ($i=$page+1; $i<=$endpage; $i++)
			$paging_str .= ' <a href="' .$pagelink.'/'.$i. '" class="link">' .$i. '</a>';
		
		$paging_str .= '&nbsp;&nbsp;&nbsp;';
		if ($page < $totalpage)
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img22_arrow_right.gif" align="absmiddle" style="cursor:pointer;" onclick="location.href=' ."'".$pagelink.'/'.($page+1)."'". '">';
		else
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img22_arrow_right.gif" align="absmiddle">';
		$paging_str .= '&nbsp;&nbsp;&nbsp;</span></td>';
	
		$paging_str .= '&nbsp;&nbsp;&nbsp;<span class="paging_total"><strong>[ </strong>T&#7893;ng c&#7897;ng: <font color="Red">' .$totalrows. '</font> k&#7871;t qu&#7843; <strong> ]</strong></span>';
		return $paging_str;
	}	
	
	//local func: phân trang dùng cho function "Tìm Kiếm"
	function paging_search($page, $totalpage, $pagelink, $totalrows)
	{
		$paging_str = '';
		$paging_str .= 'Trang: &nbsp;&nbsp;&nbsp;';
		if ($page > 1)
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img21_arrow_left.gif" align="absmiddle" style="cursor:pointer;" onclick="submitSearch(\''.$pagelink. '\','.($page-1).');return true;">';
		else
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img21_arrow_left.gif" align="absmiddle">';
		$paging_str .= '&nbsp;&nbsp;&nbsp;';
		
		// list 10 pagelink
		$startpage = 1;
		if ($page > 5)  $startpage = $page - 4;
		$endpage = $totalpage;
		if ($totalpage - $page > 5)  $endpage = $page + 5;
		for ($i=$startpage; $i<$page; $i++)
			$paging_str .= ' <a onclick="submitSearch(\''.$pagelink. '\','.$i.');return true;" class="link" href="#">'.$i.'</a>';
			//$paging_str .= ' <a onclick="submitSearch(\'"'.$pagelink. '></a>';;
		//$paging_str .= ' <a href="' .$pagelink.'/'.$i. '" class="link_selected">' .$page. '</a>';
		$paging_str .= ' <a onclick="submitSearch(\''.$pagelink. '\','.$i.');return true;" class="link_selected" href="#">'.$i.'</a>';
		for ($i=$page+1; $i<=$endpage; $i++)
			$paging_str .= ' <a onclick="submitSearch(\''.$pagelink. '\','.$i.');return true;" class="link" href="#">'.$i.'</a>';
			//$paging_str .= ' <a href="' .$pagelink.'/'.$i. '" class="link">' .$i. '</a>';
		
		$paging_str .= '&nbsp;&nbsp;&nbsp;';
		if ($page < $totalpage)
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img22_arrow_right.gif" align="absmiddle" style="cursor:pointer;" onclick="submitSearch(\''.$pagelink. '\','.($page+1).');return true;">';
		else
			$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img22_arrow_right.gif" align="absmiddle">';
		$paging_str .= '&nbsp;&nbsp;&nbsp;</span></td>';
	
		$paging_str .= '&nbsp;&nbsp;&nbsp;<span class="paging_total"><strong>[ </strong>T&#7893;ng c&#7897;ng: <font color="Red">' .$totalrows. '</font> k&#7871;t qu&#7843; <strong> ]</strong></span>';
		return $paging_str;
	}		
	
}
?>